﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MessageLib.Frame.InnerFrame;

namespace MessageLib.Frame.Base
{
    public abstract class CommandFrame:BaseFrame
    {
        public override byte[] FrameData { get;  set; } = new byte[245];
        public int BodyLength;

        public virtual byte[] FrameHead
        {
            get => new[] {FrameData[0]};
            set => FrameData[0] = value[0];
        }

        public virtual byte[] DataLength
        {
            get => new[] {FrameData[2], FrameData[1]};
            set
            {
                FrameData[2] = value[0];
                FrameData[1] = value[1];
            }
        }

        public virtual byte[] DataBody
        {
            get
            {
                if (FrameData != null && FrameData.Length > 3)
                {
                    return FrameData.Skip(3).Take(BodyLength).ToArray();
                }
                return new byte[0];
            }
            set
            {
                BodyLength = value.Length;
                DataLength = BitConverter.GetBytes(BodyLength);
                for (var i = 0; i < value.Length; i++)
                {
                    FrameData[3 + i] = value[i];
                }
            }
        }

	    public static ControlCommandFrame CreateControlCommandFrame(ControlCommand controlCommand,ControlCommandParam commandParam=0, string text="")
	    {
		    switch (controlCommand)
		    {
				case ControlCommand.PlayVoice:
				    var buffer = new List<byte> {(byte) controlCommand, (byte) commandParam};
				    switch (commandParam)
				    {
						case ControlCommandParam.Gb2312:
							buffer.AddRange(Encoding.GetEncoding("GB2312").GetBytes(text));
							break;
						case ControlCommandParam.Gbk:
							buffer.AddRange(Encoding.GetEncoding("GBK").GetBytes(text));
							break;
						case ControlCommandParam.Big5:
							buffer.AddRange(Encoding.GetEncoding("BIG5").GetBytes(text));
							break;
						case ControlCommandParam.UnicodeLe:
							buffer.AddRange(Encoding.Unicode.GetBytes(text));
							break;
						case ControlCommandParam.UnicodeBe:
							buffer.AddRange(Encoding.BigEndianUnicode.GetBytes(text));
							break;
				    }
					return new ControlCommandFrame
					{
						DataBody = buffer.ToArray()
					};
				default:
				    var f = new ControlCommandFrame
				    {
					    FrameData = new byte[245]
				    };
				    f.FrameData[0] = 0xFD;
				    f.FrameData[1] = 0x00;
				    f.FrameData[2] = 0x01;
				    f.FrameData[3] = (byte) controlCommand;
				    f.BodyLength = 1;
				    return f;
		    }
	    }

	    public static SettingCommandFrame CreateSettingCommandFrame(byte[] mac,byte[] ip,byte[]mask,byte[] gateway)
	    {
		    var f = new SettingCommandFrame();
		    var l = new List<byte> {0xFE, 0x00};
		    l.AddRange(mac);
			l.AddRange(ip);
			l.AddRange(mask);
			l.AddRange(gateway);
		    f.DataBody = l.ToArray();
		    return f;
	    }
    }
}
